package com.flextribe.display
{
	import com.flextribe.events.TribeStageEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	

	/**
	 *  Dispatched when the mouse gets on the player's stage.
	 *
	 *  @eventType com.flextribe.events.TribeStageEvent.STAGE_OVER
	 */
	[Event(name="stageOver", type="com.flextribe.events.TribeStageEvent")]

	/**
	 *  Dispatched when the mouse gets on the player's stage.
	 *
	 *  @eventType com.flextribe.events.TribeStageEvent.STAGE_OUT
	 */
	[Event(name="stageOut", type="com.flextribe.events.TribeStageEvent")]

	/**
	 * Class for additional stage watching utilities:
	 * - TribeStageEvent.STAGE_OUT
	 * - TribeStageEvent.STAGE_OVER
	 * @author llozan
	 */
	public class TribeStage extends EventDispatcher
	{
		
		public static const TRIBE_STAGE_WATCHER_NAME:String = "TribeStageWatcher";
		
		private var _parent:DisplayObject;
		private var _stage:Stage;
		
		private var _watcher:Shape;
		
		/**
		 * Default constructor  
		 * @param parent The parent object in which the StageWatcher is created.
		 * 
		 */
		public function TribeStage(parent:DisplayObject):void
		{
			_parent = parent;
			
			createChildren();
			
			if(_parent.stage != null) onAddedToStage_parent(null);
			_parent.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage_parent);
			_parent.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage_parent);
		}

		private function createChildren():void{
			_watcher = new Shape();
			_watcher.name = TRIBE_STAGE_WATCHER_NAME;
			var g:Graphics = _watcher.graphics;
			g.beginFill(0, 0);
			g.drawRect(0, 0, 10, 10);
		}

		private function onAddedToStage_parent(evt:Event):void
		{
			_stage = _parent.stage;
			_watcher.width = _stage.stageWidth;
			_watcher.height = _stage.stageHeight;
			
			_stage.addChildAt(_watcher, 0);
			_stage.addEventListener(Event.RESIZE, onResize_stage, false, 0, true);
			_stage.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver_stage, false, 0, true);
			_stage.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut_stage, false, 0, true);
			
		}

		private function onRemovedFromStage_parent(evt:Event):void
		{
			_stage.removeChild(_watcher);
			_stage.removeEventListener(Event.RESIZE, onResize_stage);
			_stage.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver_stage);
			_stage.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut_stage);
			_stage = null;
		}
		
		private function onResize_stage(evt:Event):void
		{
			_watcher.width = _stage.stageWidth;
			_watcher.height = _stage.stageHeight;
		}
		private function onMouseOver_stage(evt:MouseEvent):void
		{
			trace(evt.type, evt.target);
			this.dispatchEvent(new TribeStageEvent(TribeStageEvent.STAGE_OVER));
		}

		private function onMouseOut_stage(evt:MouseEvent):void
		{
			trace(evt.type, evt.target);
			this.dispatchEvent(new TribeStageEvent(TribeStageEvent.STAGE_OUT));
		}

		
	}
}